<template>
  <el-dialog :title="dialogTitle" :visible="visible" width="20%" @close="onDialogClose">
    <el-form ref="form" :model="ruleForm" :rules="rules" label-width="80px">
      <el-form-item label="所属学科" prop="subjectID" v-if="!detail.id && !detail.name">
        <el-select v-model="ruleForm.subjectID" placeholder="请选择" >
          <el-option  v-for="item in select" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目录名称" prop="directoryName">
        <el-input
          v-model="ruleForm.directoryName"
          placeholder="请输入目录名称"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('input', false)">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>

import { add, update } from '@/api/hmmm/directorys.js'
export default {
  name: 'dialogss',
  model: {
    prop: 'visible'
  },
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    select: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      ruleForm: {
        directoryName: '',
        subjectID: '',
        id: null
      },
      rules: {
        directoryName: [
          {
            required: true,
            message: '请输入目录名称',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
    console.log(this.$route.query.id)
  },
  computed: {
    dialogTitle () {
      return this.ruleForm.id ? '编辑部门' : '新增部门'
    },
    detail () {
      return this.$route.query || null
    }
  },
  methods: {
    getName (row) {
      this.ruleForm = { ...row }
    },
    onDialogClose () {
      this.$emit('input', false)
      this.ruleForm = {
        directoryName: '',
        subjectID: '',
        id: null
      }
    },
    async save () {
      try {
        await this.$refs.form.validate()
        // 修改
        if (this.ruleForm.id) {
          await update(this.ruleForm)
          this.$message.success('修改成功')
        } else {
          // 添加
          if (this.$route.query.id) {
            this.ruleForm.subjectID = this.$route.query.id - 0
            await add(this.ruleForm)
            this.$message.success('添加成功')
          } else {
            await add(this.ruleForm)
            this.$message.success('添加成功')
          }
        }
        this.$emit('add-succsee')
        this.$emit('input', false)
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang="less">
/deep/.el-input__inner{
  width: 260px;
}
</style>
